package org.vectrics.swing;


import javax.swing.JComponent;
import java.awt.Color;
import org.apache.log4j.*;

public class Cell {
	final private transient static Logger log = Logger.getLogger(Cell.class);
	public int ALIGN_LEFT = 0;
	public int ALIGN_CENTER = 1;
	public int ALIGN_RIGHT = 2;
	public int ALIGN_TOP = 0;
	public int ALIGN_MIDDLE = 1;
	public int ALIGN_BOTTOM = 2;
	
	private int colSpan = 1;
	private JComponent component = null;
	private int align = ALIGN_LEFT;
	private int vAlign = ALIGN_TOP;
	private Color bgColor = null;
	private TablePanel table = null;
	private Row row = null;
	
	
	
	public Cell(TablePanel table, Row row) {
		super();
		this.table = table;
		this.row = row;
	}
	
	public Cell(JComponent component, TablePanel table, Row row) {
		super();
		this.component = component;
		this.table = table;
		this.row = row;
		this.table.getPanel().add(component);
	}
	
	
	protected int getRowY() {
		return(this.table.getRowY(this.row));
	}
	
	protected void setComponentPosition() {
		int column = this.row.getColumnStart(this);
		int left = this.row.getColumnLeft(column);
		int endColumn = column + this.getColSpan();
		int width = this.row.getColumnLeft(endColumn);
		int rowY = getRowY();
		if (this.component != null) {
			this.component.setBounds(left - this.table.getXPadding(), rowY, width, row.getHeight());
		}
		if (log.isDebugEnabled()) {
			log.debug("setComponentPosition() - column    = " + column);
			log.debug("setComponentPosition() - endColumn = " + endColumn);
			log.debug("setComponentPosition() - left      = " + left);
			log.debug("setComponentPosition() - width     = " + width);
			log.debug("setComponentPosition() - rowY      = " + rowY);
		}
	}

	
	public int getColSpan() {
		return colSpan;
	}

	public void setColSpan(int colSpan) {
		if (this.colSpan != colSpan) {
			this.colSpan = colSpan;
			setComponentPosition();
		}
		else {
		}
	}


	public JComponent getComponent() {
		return component;
	}


	public void setComponent(JComponent component) {
		this.component = component;
		this.table.getPanel().add(component);		
		setComponentPosition();
	}


	public int getAlign() {
		return align;
	}

	public void setAlign(int align) {
		this.align = align;
	}

	public int getVAlign() {
		return vAlign;
	}

	public void setVAlign(int align) {
		vAlign = align;
	}

	public Color getBgColor() {
		return bgColor;
	}

	public void setBgColor(Color bgColor) {
		this.bgColor = bgColor;
	}

}
